package org.hgvc.webapp.ets.jsf.beans.admin;

import java.util.ArrayList;
import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.component.UIParameter;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import org.hgvc.webapp.ets.bo.Country;
import org.hgvc.webapp.ets.bo.Member;
import org.hgvc.webapp.ets.bo.MemberComp;
import org.hgvc.webapp.ets.bo.MemberIndAcct;
import org.hgvc.webapp.ets.bo.Membership;
import org.hgvc.webapp.ets.bo.Nationality;
import org.hgvc.webapp.ets.bo.Race;
import org.hgvc.webapp.ets.bo.Salutation;
import org.hgvc.webapp.ets.bo.State;
import org.hgvc.webapp.ets.constant.AdminConstant;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.constant.MemberConstant;
import org.hgvc.webapp.ets.constant.TerminateConstant;
import org.hgvc.webapp.ets.spring.service.MasterSetupService;
import org.hgvc.webapp.ets.spring.service.MemberService;
import org.hgvc.webapp.ets.spring.service.TerminateProcService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.SpringUtils;


public class AdminMemberCompProfileBean extends AbstractBean {

	public static final String BEAN_NAME ="adminMemberCompProfileBean";
	public static final String NAV_VIEW="adminCompMemberProfile";
	public static final String NAV_VIEW_NOMIMEE="adminCompMemberNomineeProfile";
	private MemberService service;
	private MemberComp memberComp;
	private List<MemberIndAcct> nomimeeList;
	private Member member;
	private List<SelectItem> statementTypeList;
	private List<SelectItem> yesNoSelectList;
	private String indCompcountryId;
	private List<SelectItem> countrySelectList;
	private MasterSetupService setupService;
	private String indCompStateId;
	private List<SelectItem> indCompStateSelectList;
	private List<SelectItem> compMailSelectList;
	private boolean mailFlag;
	private boolean compFlag;
	private String compMailSelect;
	private String indMailCountryId;
	private String indMailStateId;
	private List<SelectItem> indMailStateSelectList;
	private MemberIndAcct memberIndAcct;
	private List<SelectItem> salutationSelectList;
	private String deSaluationId;
	private List<SelectItem> sexSelectList;
	private List<SelectItem> maritalSelectList;
	private List<SelectItem> raceSelectList;
	private String deRaceId;
	private String deResCountryId;
	private List<SelectItem> deResStateSelectList;
	private String deResStateId;
	private String deMailSelect;
	private boolean deMailFlag;
	private boolean deCompFlag;
	private String deCompStateId;
	private List<SelectItem> deCompStateSelectList;
	private String deCompCountryId;
	private String deMailCountryId;
	private String deMailStateId;
	private List<SelectItem> deMailStateSelectList;
	private List<SelectItem> mailSelectList;
	private Membership membership;
	private boolean checkFlag = true;
	private String flag;
	private TerminateProcService terminateProcService;
	private boolean catchError;
	private boolean terminated;

	private List<SelectItem> selectNationalities;
	
	public List<SelectItem> getSelectNationalities() {
		selectNationalities=new ArrayList<SelectItem>();
		try{
			List<Nationality> list = setupService.getNationalityList();
			for (Nationality n : list){
				SelectItem selectItem = new SelectItem(n.getNationalityName(),n.getNationalityName());
				selectNationalities.add(selectItem);
			}
		}catch(BaseException be){
			initErrorBean(be);
		}
		return selectNationalities;
	}

	public boolean isTerminated() {
		if (memberComp.getMember().getStatus().equalsIgnoreCase(TerminateConstant.TERMINATED))
			terminated=true;
		else
			terminated=false;
		return terminated;
	}
	public void setTerminateProcService(TerminateProcService terminateProcService) {
		this.terminateProcService = terminateProcService;
	}
	public boolean isCheckFlag() {
		return checkFlag;
	}
	public void setCheckFlag(boolean checkFlag) {
		this.checkFlag = checkFlag;
	}
	public String getFlag() {
		return flag;
	}
	public void setFlag(String flag) {
		this.flag = flag;
	}

	public Membership getMembership() {
		return membership;
	}
	public void setMembership(Membership membership) {
		this.membership = membership;
	}
	public String getDeMailCountryId() {
		return deMailCountryId;
	}
	public void setDeMailCountryId(String deMailCountryId) {
		this.deMailCountryId = deMailCountryId;
	}
	public String getDeMailStateId() {
		return deMailStateId;
	}
	public void setDeMailStateId(String deMailStateId) {
		this.deMailStateId = deMailStateId;
	}
	public String getDeCompCountryId() {
		return deCompCountryId;
	}
	public void setDeCompCountryId(String deCompCountryId) {
		this.deCompCountryId = deCompCountryId;
	}
	public String getDeCompStateId() {
		return deCompStateId;
	}
	public void setDeCompStateId(String deCompStateId) {
		this.deCompStateId = deCompStateId;
	}
	public String getDeMailSelect() {
		return deMailSelect;
	}
	public boolean isDeMailFlag() {
		return deMailFlag;
	}
	public void setDeMailFlag(boolean deMailFlag) {
		this.deMailFlag = deMailFlag;
	}
	public boolean isDeCompFlag() {
		return deCompFlag;
	}
	public void setDeCompFlag(boolean deCompFlag) {
		this.deCompFlag = deCompFlag;
	}
	public String getDeResStateId() {
		return deResStateId;
	}
	public void setDeResStateId(String deResStateId) {
		this.deResStateId = deResStateId;
	}
	public String getDeRaceId() {
		return deRaceId;
	}
	public void setDeRaceId(String deRaceId) {
		this.deRaceId = deRaceId;
	}
	public String getDeResCountryId() {
		return deResCountryId;
	}
	public void setDeResCountryId(String deResCountryId) {
		this.deResCountryId = deResCountryId;
	}
	public String getDeSaluationId() {
		return deSaluationId;
	}
	public void setDeSaluationId(String deSaluationId) {
		this.deSaluationId = deSaluationId;
	}
	public MemberIndAcct getMemberIndAcct() {
		return memberIndAcct;
	}
	public void setMemberIndAcct(MemberIndAcct memberIndAcct) {
		this.memberIndAcct = memberIndAcct;
	}
	public String getIndMailCountryId() {
		return indMailCountryId;
	}
	public void setIndMailCountryId(String indMailCountryId) {
		this.indMailCountryId = indMailCountryId;
	}
	public String getIndMailStateId() {
		return indMailStateId;
	}
	public void setIndMailStateId(String indMailStateId) {
		this.indMailStateId = indMailStateId;
	}
	public boolean isMailFlag() {
		return mailFlag;
	}
	public void setMailFlag(boolean mailFlag) {
		this.mailFlag = mailFlag;
	}
	public boolean isCompFlag() {
		return compFlag;
	}
	public void setCompFlag(boolean compFlag) {
		this.compFlag = compFlag;
	}
	public String getCompMailSelect() {
		return compMailSelect;
	}
	public String getIndCompStateId() {
		return indCompStateId;
	}
	public void setIndCompStateId(String indCompStateId) {
		this.indCompStateId = indCompStateId;
	}
	public String getIndCompcountryId() {
		return indCompcountryId;
	}
	public void setIndCompcountryId(String indCompcountryId) {
		this.indCompcountryId = indCompcountryId;
	}
	public Member getMember() {
		return member;
	}
	public void setMember(Member member) {
		this.member = member;
	}
	public void setSetupService(MasterSetupService setupService) {
		this.setupService = setupService;
	}
	public void setService(MemberService service) {
		this.service = service;
	}
	public MemberComp getMemberComp() {
		return memberComp;
	}
	public void setMemberComp(MemberComp memberComp) {
		this.memberComp = memberComp;
	}
	public List<MemberIndAcct> getNomimeeList() {
		return nomimeeList;
	}
	public void setNomimeeList(List<MemberIndAcct> nomimeeList) {
		this.nomimeeList = nomimeeList;
	}

	/*------------------------------------------------------------------------------------*/
	public List<SelectItem> getCountrySelectList() {
		try{
			List<Country> cList = setupService.getCountryList();
			countrySelectList= new ArrayList<SelectItem>();
			for(Country c:cList){
				SelectItem item = new SelectItem(c.getCountryId(),c.getDesc());
				countrySelectList.add(item);
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return countrySelectList;
	}
	public List<SelectItem> getCompMailSelectList() {

		compMailSelectList = new ArrayList<SelectItem>();
		for(String[] array:CommonConstant.MAIL_COMP_OTHER_SELECT_LIST){
			SelectItem item = new SelectItem(array[0],SpringUtils.getMessage(array[1],new Object[]{}));
			compMailSelectList.add(item);
		}

		return compMailSelectList;
	}
	public List<SelectItem> getStatementTypeList() {

		statementTypeList = new ArrayList<SelectItem>();
		SelectItem item = new SelectItem(MemberConstant.MEMBER_STATEMENT_IND,SpringUtils.getMessage(MemberConstant.MEMBER_STATEMENT_IND, new Object[]{}));
		SelectItem item1 = new SelectItem(MemberConstant.MEMBER_STATEMENT_COMB,SpringUtils.getMessage(MemberConstant.MEMBER_STATEMENT_COMB, new Object[]{}));
		statementTypeList.add(item);
		statementTypeList.add(item1);
		return statementTypeList;
	}
	public List<SelectItem> getYesNoSelectList() {
		yesNoSelectList = new ArrayList<SelectItem>();
		for(String[] str:CommonConstant.yesnoSelectList){
			SelectItem item = new SelectItem(str[0],SpringUtils.getMessage(str[1], new Object[]{}));
			yesNoSelectList.add(item);
		}

		return yesNoSelectList;
	}
	public List<SelectItem> getIndCompStateSelectList() {
		try{
			indCompStateSelectList = new ArrayList<SelectItem>();
			if(indCompcountryId!=null&&indCompcountryId.length()>0){
				List<State> sList = setupService.getStateList(indCompcountryId);
				for(State s:sList){
					SelectItem item = new SelectItem(s.getStateId(),s.getDesc());
					indCompStateSelectList.add(item);
				}
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return indCompStateSelectList;
	}
	public void setCompMailSelect(String compMailSelect) {
		if(compMailSelect.equalsIgnoreCase(CommonConstant.MAIL_SELECT_OTHER)){
			mailFlag = false;
			compFlag = false;
		}else{
			mailFlag = true;
			if(compMailSelect.equalsIgnoreCase(CommonConstant.MAIL_SELECT_COMP)){
				memberComp.setMailAddress1(memberComp.getRegAddress1());
				memberComp.setMailAddress2(memberComp.getRegAddress2());
				memberComp.setMailPostcode(memberComp.getRegPostcode());
				memberComp.setMailCity(memberComp.getRegCity());
				memberComp.setMailCountry(memberComp.getRegCountry());
				memberComp.setMailState(memberComp.getRegState());
				compFlag = true;
			}
		}
		this.compMailSelect = compMailSelect;
	}

	public List<SelectItem> getIndMailStateSelectList() {
		try{
			indMailStateSelectList = new ArrayList<SelectItem>();
			if(indMailCountryId!=null&&indMailCountryId.length()>0){
				List<State> sList = setupService.getStateList(indMailCountryId);
				for(State s:sList){
					SelectItem item = new SelectItem(s.getStateId(),s.getDesc());
					indMailStateSelectList.add(item);
				}
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return indMailStateSelectList;
	}
	public String navViewAction(Member member){
		try{

			this.member = member;
			this.memberComp = service.getMemberCompByMemberId(member.getMemberId());
			this.nomimeeList = service.getMemberIndAcctByMemberComp(memberComp.getMemberCompId());

			if(memberComp.getMailCountry()!=null){
				indMailCountryId = memberComp.getMailCountry().getCountryId();
			}else{
				indMailCountryId = null;
			}
			if(memberComp.getMailState()!=null){
				indMailStateId = memberComp.getMailState().getStateId();
			}else{
				indMailStateId = null;
			}

			if(memberComp.getRegCountry()!=null){
				indCompcountryId = memberComp.getRegCountry().getCountryId();
			}else{
				indCompcountryId = null;
			}
			if(memberComp.getRegState()!=null){
				indCompStateId = memberComp.getRegState().getStateId();
			}else{
				indCompStateId = null;
			}

			compMailSelect=CommonConstant.MAIL_SELECT_OTHER;
			mailFlag = false;
			compFlag = false;

		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}

		return NAV_VIEW.concat("_"+getRoleName());
	}
	
	public void navNomimeeActionListener(ActionEvent actionEvent){
		UIParameter param = (UIParameter)actionEvent.getComponent().findComponent("dependantId");
		String id = (String)param.getValue();
		for(MemberIndAcct dependant:nomimeeList){
			if(id.equalsIgnoreCase(dependant.getMemberIndAcctId())){
				memberIndAcct = dependant;
				break;
			}
		}
	}
	
	public String navNomimeeAction(){

		deMailSelect=CommonConstant.MAIL_SELECT_OTHER;
		deMailFlag = false;
		deCompFlag = false;
		

		if(memberIndAcct.getMailCountry()!=null)
			deMailCountryId = memberIndAcct.getMailCountry().getCountryId();
		else
			deMailCountryId = null;
		if(memberIndAcct.getMailState()!=null)
			deMailStateId = memberIndAcct.getMailState().getStateId();
		else
			deMailStateId = null;
		if(memberIndAcct.getCompCountry()!=null)
			deCompCountryId = memberIndAcct.getCompCountry().getCountryId();
		else
			deCompCountryId = null;
		if(memberIndAcct.getCompState()!=null)
			deCompStateId = memberIndAcct.getCompState().getStateId();
		else
			deCompStateId = null;

		if(memberIndAcct.getResCountry()!=null)
			deResCountryId = memberIndAcct.getResCountry().getCountryId();
		else
			deResCountryId = null;
		if(memberIndAcct.getResState()!=null)
			deResStateId = memberIndAcct.getResState().getStateId();
		else
			deResStateId = null;
		if(memberIndAcct.getRace()!=null)
			deRaceId = memberIndAcct.getRace().getRaceId();
		else
			deRaceId = null;
		if(memberIndAcct.getSalutation()!=null){
			deSaluationId = memberIndAcct.getSalutation().getSalutationId();
		}else{
			deSaluationId = null;
		}

		return NAV_VIEW_NOMIMEE.concat("_"+getRoleName());
	}
	
	

	public String updateNomimeeAction(){
		/*if(nomimeeList.size()==membership.getMembershipCategory().getDependantLimitInt()){
				initErrorBean(SpringUtils.getMessage("error.dependant.limit", new Object[]{}));
				return NAV_CREATE_NOMIMEE;
			}
			//validate dependant ic no
			for(MemberIndAcct dependant:nomimeeList){
				if(dependant.getNewIcNo().equalsIgnoreCase(memberIndAcct.getNewIcNo())){
					initErrorBean(SpringUtils.getMessage("error.duplicate.ic.no.dependant.found", new Object[]{}));
					return NAV_CREATE_NOMIMEE;
				}
			}*/

		if(deMailCountryId!=null&&deMailCountryId.length()>0)
			memberIndAcct.setMailCountry(new Country(deMailCountryId));
		if(deMailStateId!=null&&deMailStateId.length()>0)
			memberIndAcct.setMailState(new State(deMailStateId));
		if(deCompCountryId!=null&&deCompCountryId.length()>0)
			memberIndAcct.setCompCountry(new Country(deCompCountryId));
		if(deCompStateId!=null&&deCompStateId.length()>0)
			memberIndAcct.setCompState(new State(deCompStateId));
		if(deResCountryId!=null&&deResCountryId.length()>0)
			memberIndAcct.setResCountry(new Country(deResCountryId));
		if(deResStateId!=null&&deResStateId.length()>0)
			memberIndAcct.setResState(new State(deResStateId));
		if(deRaceId!=null&&deRaceId.length()>0)
			memberIndAcct.setRace(new Race(deRaceId));
		if(deSaluationId!=null&&deSaluationId.length()>0){
			memberIndAcct.setSalutation(new Salutation(deSaluationId));
		}

		return NAV_VIEW.concat("_"+getRoleName());
	}

	public List<SelectItem> getSalutationSelectList() {
		try{
			List<Salutation> sList = setupService.getSalutationList();
			salutationSelectList= new ArrayList<SelectItem>();
			for(Salutation s:sList){
				SelectItem item = new SelectItem(s.getSalutationId(),s.getDesc());
				salutationSelectList.add(item);
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return salutationSelectList;
	}
	public List<SelectItem> getSexSelectList() {
		sexSelectList = new ArrayList<SelectItem>();
		for(String[] array:CommonConstant.genderSelectList){
			SelectItem item = new SelectItem(array[0],SpringUtils.getMessage(array[1],new Object[]{}));
			sexSelectList.add(item);
		}
		return sexSelectList;
	}
	public List<SelectItem> getMaritalSelectList() {
		maritalSelectList = new ArrayList<SelectItem>();
		for(String[] array:CommonConstant.maritalStatusSelectList){
			SelectItem item = new SelectItem(array[0],SpringUtils.getMessage(array[1],new Object[]{}));
			maritalSelectList.add(item);
		}
		return maritalSelectList;
	}
	public List<SelectItem> getRaceSelectList() {
		try{
			List<Race> rList = setupService.getRaceList();
			raceSelectList= new ArrayList<SelectItem>();
			for(Race r:rList){
				SelectItem item = new SelectItem(r.getRaceId(),r.getDesc());
				raceSelectList.add(item);
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return raceSelectList;
	}
	public List<SelectItem> getDeResStateSelectList() {
		try{
			deResStateSelectList = new ArrayList<SelectItem>();
			if(deResCountryId!=null&&deResCountryId.length()>0){
				List<State> sList = setupService.getStateList(deResCountryId);
				for(State s:sList){
					SelectItem item = new SelectItem(s.getStateId(),s.getDesc());
					deResStateSelectList.add(item);
				}
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return deResStateSelectList;
	}
	public void setDeMailSelect(String deMailSelect) {
		if(deMailSelect.equalsIgnoreCase(CommonConstant.MAIL_SELECT_OTHER)){
			deMailFlag = false;
			deCompFlag = false;
		}else{
			deMailFlag = true;
			if(deMailSelect.equalsIgnoreCase(CommonConstant.MAIL_SELECT_COMP)){
				memberIndAcct.setMailAddress1(memberIndAcct.getCompAddress1());
				memberIndAcct.setMailAddress2(memberIndAcct.getCompAddress2());
				memberIndAcct.setMailPostcode(memberIndAcct.getCompPostcode());
				memberIndAcct.setMailCity(memberIndAcct.getCompCity());
				memberIndAcct.setMailCountry(memberIndAcct.getCompCountry());
				memberIndAcct.setMailState(memberIndAcct.getCompState());
				deCompFlag = true;
			}else if(deMailSelect.equalsIgnoreCase(CommonConstant.MAIL_SELECT_RES)){
				memberIndAcct.setMailAddress1(memberIndAcct.getResAddress1());
				memberIndAcct.setMailAddress2(memberIndAcct.getResAddress2());
				memberIndAcct.setMailPostcode(memberIndAcct.getResPostcode());
				memberIndAcct.setMailCity(memberIndAcct.getResCity());
				memberIndAcct.setMailCountry(memberIndAcct.getResCountry());
				memberIndAcct.setMailState(memberIndAcct.getResState());
				deCompFlag = false;
			}
		}
		this.deMailSelect = deMailSelect;
	}

	public List<SelectItem> getDeCompStateSelectList() {
		try{
			deCompStateSelectList = new ArrayList<SelectItem>();
			if(deCompCountryId!=null&&deCompCountryId.length()>0){
				List<State> sList = setupService.getStateList(deCompCountryId);
				for(State s:sList){
					SelectItem item = new SelectItem(s.getStateId(),s.getDesc());
					deCompStateSelectList.add(item);
				}
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return deCompStateSelectList;
	}
	public List<SelectItem> getDeMailStateSelectList() {
		try{
			deMailStateSelectList = new ArrayList<SelectItem>();
			if(deMailCountryId!=null&&deMailCountryId.length()>0){
				List<State> sList = setupService.getStateList(deMailCountryId);
				for(State s:sList){
					SelectItem item = new SelectItem(s.getStateId(),s.getDesc());
					deMailStateSelectList.add(item);
				}
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return deMailStateSelectList;
	}
	public List<SelectItem> getMailSelectList() {

		mailSelectList = new ArrayList<SelectItem>();
		for(String[] array:CommonConstant.MAIL_SELECT_LIST){
			SelectItem item = new SelectItem(array[0],SpringUtils.getMessage(array[1],new Object[]{}));
			mailSelectList.add(item);
		}

		return mailSelectList;
	}
	
	public String updateMemberProfileAction(){
		try{
			
			if(indMailCountryId!=null&&indMailCountryId.length()>0)
				memberComp.setMailCountry(new Country(indMailCountryId));
			if(indMailStateId!=null&&indMailStateId.length()>0)
				memberComp.setMailState(new State(indMailStateId));
			if(indCompcountryId!=null&&indCompcountryId.length()>0)
				memberComp.setRegCountry(new Country(indCompcountryId));
			if(indCompStateId!=null&&indCompStateId.length()>0)
				memberComp.setRegState(new State(indCompStateId));
			
			if (compMailSelect.equalsIgnoreCase(CommonConstant.MAIL_SELECT_COMP)){
				this.memberComp.setMailAddress1(this.memberComp.getRegAddress1());
				this.memberComp.setMailAddress2(this.memberComp.getRegAddress2());
				this.memberComp.setMailAddress3(this.memberComp.getRegAddress3());
				this.memberComp.setMailPostcode(this.memberComp.getRegPostcode());
				this.memberComp.setMailCity(this.memberComp.getRegCity());
				this.memberComp.setMailCountry(this.memberComp.getRegCountry());
				this.memberComp.setMailState(this.memberComp.getRegState());
			}
			
			service.updateMemberCompProfileAction(member, memberComp, nomimeeList, getThisUser());
			
			return AdminPortalBean.NAV_ADMIN_PORTAL.concat("_"+getRoleName());
		}catch (BaseException be) {
			be.printStackTrace();
			initErrorBean(be);
		}
		
		return NAV_VIEW.concat("_"+getRoleName());
	}
	
	public String navBackPortalAction(){
		return AdminPortalBean.NAV_ADMIN_PORTAL.concat("_"+getRoleName());
	}
	
	public String navBackAction(){
		return NAV_VIEW.concat("_"+getRoleName());
	}
	
	public void validator(FacesContext context,UIComponent comp,Object obj){
		String value = (String)obj;
		if(value.equalsIgnoreCase("1")){
			checkFlag = false;
		}else{
			checkFlag = true;
		}
	}
	
	public String terminateAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		
		if (memberComp.getMember().getMemberType().equalsIgnoreCase(MemberConstant.MEMBER_STATUS_MEMBER))
			outcome=outcome.concat("_MEMBER");
		else
			outcome=outcome.concat("_PROSPECT");
		
		outcome=outcome.concat("_"+getRoleName());
		if (this.catchError)
			outcome=null;
		return outcome;
	}
	public void terminateActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			if (memberComp.getMember().getMemberType().equalsIgnoreCase(MemberConstant.MEMBER_STATUS_PENDING_MEMBER))//Prospect
			   this.terminateProcService.terminateProspect(memberComp.getMember(), getThisUser());
			else
				this.terminateProcService.terminateMember(memberComp.getMember(), getThisUser());	
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
}
